//
//  BQHomeViewController.m
//  BeeQuick
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQHomeViewController.h"
#import "BQCollectionViewController.h"
#import "BQNavigationController.h"
#import "BQTopGuideView.h"

#import "BQSaoYiSaoViewController.h"
#import "BQSaoMaDetailViewController.h"
#import "BQSearchController.h"
#import <CoreLocation/CoreLocation.h>

//#import <PYSearch.h>
@interface BQHomeViewController ()<CLLocationManagerDelegate,BQCollectionViewControllerDelegate,BQTopGuideViewDelegate>




// 位置管理者
@property (nonatomic, strong) CLLocationManager *manager;

// 编码器
@property (nonatomic, strong) CLGeocoder *geocoder;


@property(weak,nonatomic)BQTopGuideView *guideView;


@property(strong,nonatomic)NSArray *bigModelArr;
@end

@implementation BQHomeViewController


/**
 懒加载
 @return 返回一个位置管理者
 */
- (CLLocationManager *)manager
{
    if (_manager == nil) {
        _manager = [[CLLocationManager alloc]init];
        [_manager requestAlwaysAuthorization];
        _manager.delegate = self;
    }
    return _manager;
}


/**
 @return 返回编码器对象
 */
- (CLGeocoder *)geocoder
{
    if (_geocoder == nil) {
        _geocoder = [[CLGeocoder alloc]init];
    }
    return _geocoder;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];
    [self setupUI];
//    [self setupTextField];

    
    // 添加通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushNextVC:) name:@"pushNextVC" object:nil];
    
    // 开始实现定位的功能
    [self.manager startUpdatingLocation];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}


#pragma mark 通知的监听方法
- (void)pushNextVC:(NSNotification *)not
{
    NSString *urlString = not.object;
    BQSaoMaDetailViewController *vc = [[BQSaoMaDetailViewController alloc]init];
    vc.urlString = urlString;
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)setupUI
{
    //创建collectionView
    BQCollectionViewController *collectionVc = [[BQCollectionViewController alloc]init];
    
    //设置自己定义的代理
    collectionVc.delegate = self;
    
    [self addChildViewController:collectionVc];
    
    [self.view addSubview:collectionVc.collectionView];
    
    [collectionVc.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.edges.equalTo(self.view);
        make.edges.mas_equalTo(UIEdgeInsetsMake(-64, 0, 0, 0));
        //        make.edges.mas_equalTo(UIEdgeInsetsMake(-74, 0, 0, 0));
    }];

    BQTopGuideView *topView = [[BQTopGuideView alloc]init];
    //关联
    self.guideView = topView;
    
    topView.delegate = self;
    
    [self.view addSubview:topView];
    
    //约束
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = locations.lastObject;
    
    // 地理编码
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placemark = placemarks.lastObject;
        
        self.guideView.textField.text  = placemark.name;
    }];
    [manager stopUpdatingLocation];
}




#pragma mark - 实现collection的代理
- (void)showMyScollcontentoffSizeY:(BQCollectionViewController *)collectionVc andOffSetY:(float)offsetY withAlpha:(CGFloat)alpha
{
    
    [self.guideView setContainerViewAlpha:alpha];
}



#pragma mark - guideView的代理   -

//MARK: - 实现扫一扫的.

- (void)pushToScancodeController
{
    NSLog(@"我是扫一扫");
    
    // 跳转控制器
    BQSaoYiSaoViewController *vc = [[BQSaoYiSaoViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}


//MARK: - 实现搜一搜.
- (void)presentSearchViewController
{
    NSLog(@"我是搜一搜");
    
    BQSearchController *searchVc = [[BQSearchController alloc]init];
    
    [self.navigationController pushViewController:searchVc animated:YES];
    
}

//MARK: - 定位的功能.

- (void)pushToLocaling
{
    NSLog(@"我是定位功能");
}





#pragma mark - 代理方法
- (void)transfromMyBigModel:(NSArray *)array
{
    self.bigModelArr = array.copy;
    
    
}
@end
